package com.sundog.sundroid.testclasses;

import java.util.Scanner;

import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.PacketListener;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.filter.PacketFilter;
import org.jivesoftware.smack.filter.PacketTypeFilter;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Packet;

import android.util.Log;

import com.sundog.sundroid.scrumMaster;

public class AdminSimulator {

	/** Boolean to stop executing thread after a message is received */
	boolean messageReceived = false;
	public String User;
	
	/** Connection configuration for XMPP messaging */
	private final ConnectionConfiguration connConfig =
		 new ConnectionConfiguration("talk.google.com", 80, "jabber.org"); 
	
	/** Establishes XMPP connection using the connection configuration */
	public final XMPPConnection connection = new XMPPConnection(connConfig);
	
	/** Create a packet filter to respond to messages from a single user */
	PacketFilter filter = new PacketTypeFilter(Message.class);
	
	/** Create a packet listener to process received packets */
	PacketListener myListener = new PacketListener() {

		public void processPacket(Packet packet) {
				Message msg = (Message) packet;

				Log.d("***", "Admin received message: " + msg.getBody() + " from " + msg.getFrom());
				User = msg.getFrom();
				//Log.d("***", "checking getfrom" + User);
				setUserStatus();  // set the status of current user to online.
			    
				//scMaster.updateScreenDisplay();
				// Create a scanner and parse the user name from the message 
				Scanner in = new Scanner(msg.getFrom());
				in.useDelimiter("/");
				String user = in.next();
				
				Log.d("***", "checking getfrom " + User);
				Message mess = new Message();
				mess.setTo(user);
				mess.setType(Message.Type.headline);
				mess.setBody("Admin received user message");
				//connection.sendPacket(mess);
				Log.d("***", "Admin sent message " + mess.getBody());
				messageReceived = true;
				//connection.disconnect();
				//Log.d("***", "Admin disconnected");
		}};
	
	/**
	 * Start executing messaging thread
	 */
		
	public void start() {
		
		/** Thread for receiving messages from user */
		final Thread t = new Thread() {
			public void run() {
				
				// Connect to server and add packet listener. Parameters for the packet listener were created above
				connectToServer();
				connection.addPacketListener(myListener, filter);
				Log.d("***", "Admin listener added");

				// Run the thread until a message is received
				while(!messageReceived) {
					try {
						Thread.sleep(3000);
						Log.d("***", "Admin waiting...");
					} catch(Exception e) {
						Log.d("EXCEPTION", e.getMessage());
					}
				}
			}};
		t.start();
	}
	
	/**
	 * Connect to the Jabber server and send a message to a user
	 */
	private void connectToServer() {

        // Print out connection information
		Log.d("***", "Admin Host =" + connConfig.getHost());
		Log.d("***", "Admin Service Name =" + connConfig.getServiceName());
		Log.d("***", "Admin Security Mode =" + connConfig.getSecurityMode());

		try {	
			// Connect to the server and log in with user name and password
			Log.d("***", "Admin Connect...");
			connection.connect();
			connection.login("demosundog@gmail.com", "sundog101");
			Log.d("***", "Admin Connected as: " );

		} catch (XMPPException e) {
			Log.d("***", "XMPPException");
		}
	}
	
	public void sendMessageToUser(String user){

        // Print out connection information
		Log.d("***", "Admin Host =" + connConfig.getHost());
		Log.d("***", "Admin Service Name =" + connConfig.getServiceName());
		Log.d("***", "Admin Security Mode =" + connConfig.getSecurityMode());

		try {	
			// Connect to the server if not already connected and log in with user name and password from previous activity
			if(!connection.isConnected()) {
				
				Log.d("***", "admin connect...");
				connection.connect();
				connection.login("demosundog@gmail.com","sundog101");
				
				//connection.login(this.getIntent().getExtras().get(USERNAME).toString(), this.getIntent().getExtras().get(PASSWORD).toString());
				Log.d("***", "admin connected");
			}
		} catch (XMPPException e) {
			Log.d("***", "XMPPException");
		}
		
		// Add a packet listener to the connection. The parameters were created above
		connection.addPacketListener(myListener, filter);
		
		// Create a new message, set the message properties, and send the message
		Message message = new Message();
		message.setType(Message.Type.headline);
		message.setTo(user);
		message.setBody("" + scrumMaster.stid );
	    connection.sendPacket(message);
	    Log.d("***", "Admin sent message");
	}
	
	private void setUserStatus()
	{
		String[] temp = User.split("/");
		User = temp[0];
		 Log.d("***", "checking Status 1");
		
		for(int i = 0; i <scrumMaster.userNames.length;i++ ) {
			Log.d("***", "checking Status 2" + User);
			
			if(User.equalsIgnoreCase(scrumMaster.userNames[i])) {
				
				scrumMaster.userOnlineStatus[i]="***"+scrumMaster.userNames[i]; // set user online status to true
				Log.d("***", "checking Status " + scrumMaster.userOnlineStatus[i]); 
			}
		}
	}
}
